package com.guiyang.practice.mess1.service.impl;

import com.guiyang.practice.mess1.entity.MessageRecord;
import com.guiyang.practice.mess1.mapper.MessageRecordMapper;
import com.guiyang.practice.mess1.service.MessageRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MessageRecordServiceImpl implements MessageRecordService {

    @Autowired
    private MessageRecordMapper messageRecordMapper;

    @Override
    public List<MessageRecord> getAllRecords() {
        return messageRecordMapper.findAll();
    }

    @Override
    public List<MessageRecord> findByCondition(Map<String, Object> params) {
        // 处理分页参数
        Integer page = (Integer) params.get("page");
        Integer size = (Integer) params.get("size");

        List<MessageRecord> allRecords = messageRecordMapper.findByCondition(params);

        if (page != null && size != null && page > 0 && size > 0) {
            // 计算起始位置（简单的内存分页）
            int start = (page - 1) * size;

            // 简单的内存分页
            if (allRecords.size() > start) {
                int end = Math.min(start + size, allRecords.size());
                return allRecords.subList(start, end);
            }
            return java.util.Collections.emptyList();
        }

        return allRecords;
    }

    @Override
    public MessageRecord getById(Long id) {
        return messageRecordMapper.findById(id);
    }

    @Override
    public boolean sendMessage(MessageRecord messageRecord) {
        try {
            // 设置默认值
            if (messageRecord.getStatus() == null) {
                messageRecord.setStatus(1); // 默认状态：已发送
            }

            // 设置发送时间（如果未设置）
            if (messageRecord.getSendTime() == null) {
                messageRecord.setSendTime(new Date());
            }

            // 设置接收者类型默认值
            if (messageRecord.getReceiverType() == null) {
                messageRecord.setReceiverType(3); // 默认全部用户
            }

            // 设置创建时间
            if (messageRecord.getCreateTime() == null) {
                messageRecord.setCreateTime(new Date());
            }

            return messageRecordMapper.insert(messageRecord) > 0;
        } catch (Exception e) {
            throw new RuntimeException("发送消息失败: " + e.getMessage(), e);
        }
    }

    @Override
    public int countByMessageType(String messageType) {
        return messageRecordMapper.countByType(messageType);
    }

    @Override
    public boolean markAsRead(Long id) {
        try {
            // 使用新的 updateStatus 方法
            return messageRecordMapper.updateStatus(id, 2) > 0;
        } catch (Exception e) {
            throw new RuntimeException("标记已读失败: " + e.getMessage(), e);
        }
    }

    @Override
    public boolean deleteRecord(Long id) {
        try {
            // 使用新的 updateStatus 方法
            return messageRecordMapper.updateStatus(id, 3) > 0;
        } catch (Exception e) {
            throw new RuntimeException("删除消息失败: " + e.getMessage(), e);
        }
    }
}